package com.gf.snoopy.service;

import java.util.List;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gf.snoopy.domain.Company;
import com.gf.snoopy.dto.CompanyDto;
import com.gf.snoopy.mapper.CompanyMapper;
import com.gf.snoopy.platform.exception.PokeErrorCode;
import com.gf.snoopy.platform.exception.PokeException;
import com.gf.snoopy.util.ConvertUtil;
import com.gf.snoopy.util.page.Page;
import com.gf.snoopy.util.page.PagedList;
import com.gf.snoopy.util.page.PaginationUtil;

@Service
public class CompanyService {

	@Resource
	private CompanyMapper companyMapper;

	/**
	 * 分页查询项目下的子项目
	 * @param projectId
	 * @param query
	 * @param page
	 * @return
	 * @throws PokeException
	 */
	public PagedList<CompanyDto> getProjectPage(CompanyDto query,Page page) throws PokeException{
		if(query==null || page==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"查询参数错误");
		}
		final Company dbQuery = ConvertUtil.convert(query,Company.class);
		PagedList<Company> voteInCondition =  PaginationUtil.pagedQuery(page, new Callable<List<Company>>() {            
            public List<Company> call() throws Exception {
                return companyMapper.queryByCondition(dbQuery);
            }
        });
		
		return new PagedList<CompanyDto>(voteInCondition.getPage(),
				ConvertUtil.convertList(voteInCondition.getList(),
						CompanyDto.class));
	}
	
	/**
	 * 新增记录
	 * @param data
	 * @throws PokeException
	 */
	public void save(CompanyDto data) throws PokeException{
		Company convert = ConvertUtil.convert(data, Company.class);
		if(convert==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"subProject数据出错");
		}
		companyMapper.insert(convert);
	}
	
	/**
	 * 更新记录
	 * @param data
	 * @throws PokeException
	 */
	public void update(CompanyDto data) throws PokeException{
		Company convert = ConvertUtil.convert(data, Company.class);
		if(convert==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"subProject数据出错");
		}
		companyMapper.updateByPrimaryKeySelective(convert);
	}
	
	/**
	 * 查找结果
	 * @param id
	 * @return
	 * @throws PokeException
	 */
	public CompanyDto findById(Integer id) throws PokeException{
		if(id==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"查询的subProjectId为空");
		}
		Company project = companyMapper.selectByPrimaryKey(id);
		return ConvertUtil.convert(project, CompanyDto.class);
	}
	
	/**
	 * 根据Id删除记录
	 * @param id
	 * @throws PokeException
	 */
	public void deleteById(Integer id) throws PokeException{
		if(id==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"删除的subProjectId为空");
		}
		companyMapper.deleteByPrimaryKey(id);
	}
	

	/**
	 * 查询所有机构
	 * @return
	 */
	public List<CompanyDto> findAllCompany() {
		List<Company> results = companyMapper.queryByCondition(new Company());
		return ConvertUtil.convertList(results, CompanyDto.class);
	}
	
	public CompanyMapper getCompanyMapper() {
		return companyMapper;
	}

	public void setCompanyMapper(CompanyMapper companyMapper) {
		this.companyMapper = companyMapper;
	}

	
}
